<template>
  <div class="box">
    <div class="querybox">
      <el-input
        v-model="filters.searchBy"
        placeholder="请输入查询内容"
        style="width: 200px; right: 10px;"
        @keyup.enter.native="pbe_dimensionList(1)"
      />
      <el-button
        type="primary"
        @click="pbe_dimensionList(1)"
      >查询</el-button>
      <el-button
        type="primary"
        @click="add"
      >新增</el-button>
    </div>
    <el-table
      :data="tableData"
      border
    >
      <el-table-column
        prop="code"
        label="code"
      />
      <el-table-column
        prop="name"
        label="名称"
      />
      <el-table-column
        prop="defaultValueType"
        label="defaultValueType"
      />
      <el-table-column
        prop="maxMonth"
        label="最大适用月龄"
      />
      <el-table-column
        prop="recommendTips"
        label="recommendTips"
        show-overflow-tooltip
      />
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <div>{{ timeTranslateHMS(scope.row.created) }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-text="启用"
            inactive-text="禁用"
            @change="statusChange(scope.row)"
          />
        </template>
      </el-table-column> -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="viewUser(scope.row.id, scope.index)"
          >查看</el-button>
          <!-- <el-button type="text" size="small" @click="updUser(scope.row, scope.index)">修改</el-button> -->
          <Del-tip
            type="text"
            size="small"
          >
            <el-button type="primary" size="mini" @click="delUser(scope.row.id)">确定</el-button>
          </Del-tip>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-row style="margin-top: 10px;">
      <el-pagination
        :total="total"
        :current-page="filters.pageNum"
        :page-size="filters.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleSizeChange"
      />
    </el-row> -->

    <el-dialog
      :title="editType === 'add' ? '新增' : '查看'"
      :visible.sync="dialogFormVisible"
      :before-close="beforeClose"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        :inline="true"
      >
        <el-form-item
          label="code"
          prop="code"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.code"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="名称"
          prop="name"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.name"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="defaultValueType"
          prop="defaultValueType"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.defaultValueType"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="最大适用月龄"
          prop="maxMonth"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.maxMonth"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="recommendTips"
          prop="recommendTips"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.recommendTips" type="textarea" :autosize="{ minRows: 1, maxRows: 1}" />
        </el-form-item>
        <el-form-item
          label="创建时间"
          prop="created"
          :label-width="formLabelWidth"
        >
          <el-date-picker
            v-model="form.created"
            type="date"
            placeholder="选择时间"
            disabled
            style="width: 185px;"
          />
        </el-form-item>
      </el-form>
      <!-- <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="username" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off" />
        </el-form-item>
      </el-form> -->
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button
          type="primary"
          @click="closeDialogForm('form')"
        >确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { pbe_dimensionList, pbe_dimensionInfo, pbe_dimensionEdit, childrenDelete } from '@/api/dimension'
import { bizorg_trans } from '@/api/org'
import { productList } from '@/api/product'
import { timeTranslateHMS } from '@/utils/formatdate'
// import Upload from '@/components/Upload'
export default {
  name: 'Documentation',
  components: {
    // Upload
  },
  data() {
    return {
      options: [],
      timeTranslateHMS,
      filters: {
        mobile: '',
        username: '',
        searchBy: '',
        pageNum: 1,
        pageSize: 10
      },
      form: {
        code: '',
        name: '',
        defaultValueType: '',
        maxMonth: '',
        recommendTips: ''
      },
      total: 0,
      rules: {
        code: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        defaultValueType: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        maxMonth: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        recommendTips: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      tableData: [],
      formLabelWidth: '120px',
      dialogFormVisible: false,
      query: '',
      editType: null
    }
  },
  mounted() {
    this.pbe_dimensionList()
    // this.productList()
  },
  methods: {
    bizOrgChange(orgId) {
      orgId = '4028808a748b6e4f01748b71b6bf0002'
      bizorg_trans({ orgId }).then((res) => {
      })
    },
    // dialog关闭事件
    beforeClose() {
      this.resetForm('form')
    },
    // logo上传成功监听
    onSuccess(res) {
      this.form.imgUrl = res
    },
    // 生成随机数
    randomStr() {
      const str = '1234567890abcdefghijkmnpqrstuvwxyz'.split('')
      let result = ''
      for (let i = 0; i < 8; i++) {
        result += str[Math.floor((Math.random() * (str.length - 1)) + 1)]
      }
      return result
    },
    // 新增操作
    add() {
      this.form.code = this.randomStr()
      this.editType = 'add'
      this.dialogFormVisible = true
    },
    // 分页切换监听
    handleSizeChange(pageNum) {
      this.filters.pageNum = pageNum
      this.pbe_dimensionList()
    },
    // 用户列表
    pbe_dimensionList(num) {
      // const pageNum = this.filters.pageNum - 1
      const pageNum = (num || this.filters.pageNum) - 1
      pbe_dimensionList({ ...this.filters, pageNum }).then(res => {
        const result = res.data
        this.tableData = result
        // this.tableData = result.content
        this.total = result.totalElements
      }).catch(err => {
        console.log(err)
      })
    },
    // 删除用户
    delUser(id) {
      this.editType = 'delete'
      // this.tableData.splice(row, 1)
      // this.$message({
      //   message: '删除成功！',
      //   type: 'success'
      // })
      childrenDelete({ childrenId: id }).then((res) => {
        this.$message({
          message: '删除成功！',
          type: 'success'
        })
        this.pbe_dimensionList()
      })
    },
    // 修改用户
    updUser(row, index) {
      this.editType = 'update'
      this.dialogFormVisible = true
    },
    // 查看用户
    viewUser(id) {
      this.editType = 'view'
      this.dialogFormVisible = true
      this.pbe_dimensionInfo(id)
    },
    // 用户详情
    pbe_dimensionInfo(itemId) {
      pbe_dimensionInfo({ itemId }).then(res => {
        // res.data.org.logo = 'https://dss1.baidu.com/70cFfyinKgQFm2e88IuM_a/forum/pic/item/bd315c6034a85edf3d432b7d44540923dc5475a5.jpg'
        this.form = res.data
        // let result = res.data;
        // this.tableData = result.users;
      }).catch(err => {
        console.log(err)
      })
    },
    // 弹窗关闭触发函数
    closeDialogForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.pbe_dimensionEdit(formName)
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 新增用户
    pbe_dimensionEdit(formName) {
      const obj = { ...this.form }
      obj.created = timeTranslateHMS(obj.created, null, '/')
      // obj.birthday = timeTranslateHMS(obj.birthday, null, '/')
      pbe_dimensionEdit(obj).then((res) => {
        this.$message({
          message: '修改成功！',
          type: 'success'
        })
        this.pbe_dimensionList()
        this.resetForm(formName)
        // this.dialogFormVisible = false
      })
    },
    statusChange(row) {
      // {id: row.id, status: row.status}
    },
    // 产品列表
    productList() {
      // const pageNum = this.filters.pageNum - 1
      // { ...this.filters, pageNum }
      productList().then(res => {
        const result = res.data.content
        const arr = []
        for (let i = 0; i < result.length; i++) {
          if (result[i].name) {
            arr.push(result[i])
          }
        }
        this.options = arr
        // this.tableData = result.content
        // this.total = result.totalPages
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  margin: 20px 30px;
  .form {
    margin-top: 50px;
  }
  .querybox {
    text-align: right;
    margin-bottom: 20px;
  }
}
.box-card {
  margin-bottom: 15px;
}
</style>
